/*
 * xpfsm.h
 *
 *  Created on: 2010-05-29
 *      Author: lxb
 */

#ifndef _XPFSM_H_INCLUDED_
#define _XPFSM_H_INCLUDED_

#ifdef __cplusplus
extern "C" {
#endif

/******************************************************************************
 * <Includes>
 *****************************************************************************/

/******************************************************************************
 * <Defines>
 *****************************************************************************/
#define STATEMACHINE_ERROR -1
/******************************************************************************
 * <Enums>
 *****************************************************************************/
/******************************************************************************
 * <Typedefs>
 *****************************************************************************/
typedef void (*xpfsm_func)(void *);
typedef void* xpfsm_func_context;

typedef struct xpfsm_transition xpfsm_transition;
struct xpfsm_transition
{
    int destState;
    xpfsm_func callback;
    xpfsm_func_context callbackCtx;

};


typedef struct xpfsm_state xpfsm_state;
struct xpfsm_state
{
     xpfsm_func enterCallback;
     xpfsm_func_context enterCallbackCtx;

     xpfsm_func exitCallback;
     xpfsm_func_context exitCallbackCtx;

     xpfsm_transition *transitionTable;
};

typedef struct xpfsm_context xpfsm_context;
struct xpfsm_context
{
    int curState;
    xpfsm_state *stateTable;

    xpfsm_func transCallback;
    xpfsm_func_context transCallbackCtx;
};


/******************************************************************************
 * <API declarations>
 *****************************************************************************/
xpfsm_context* xpfsm_create();
xpfsm_context* xpfsm_createx(xpfsm_state *stateTable, int initialState);
void xpfsm_delete(xpfsm_context *fsmCtx);
void xpfsm_set_statetable(xpfsm_context *fsmCtx, xpfsm_state *stateTable);
void xpfsm_set_transitiontable(xpfsm_context *fsmCtx, int state, xpfsm_transition *transitionTable);
void xpfsm_process_event(xpfsm_context *fsmCtx, unsigned char e);
void xpfsm_set_state_enter(xpfsm_context *fsmCtx, int state, xpfsm_func enterCallback, xpfsm_func_context enterCallbackCtx);
void xpfsm_set_state_exit(xpfsm_context *fsmCtx, int state, xpfsm_func exitCallback, xpfsm_func_context exitCallbackCtx);
void xpfsm_set_transition_func(xpfsm_context *fsmCtx, xpfsm_func func, xpfsm_func_context funcCtx);
int xpfsm_get_state(xpfsm_context *fsmCtx);

#ifdef __cplusplus
}
#endif

#endif /* _XPFSM_H_INCLUDED_ */








